<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot-scope="{ row }" slot="menu">
        <el-button icon="el-icon-view" size="small" type="text" @click="rowView(row)">查看</el-button>
      </template>
    </avue-crud>
    <el-dialog ref="dialog" title="查看" append-to-body :visible.sync="showDialog" :close-on-click-modal="false">
      <avue-crud :option="detailOption" :table-loading="detailLoading" :data="detailData" ref="crud" @on-load="onDetailLoad"> </avue-crud>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList, getListTable, getDetail, add, update, remove } from '@/api/warehouse/material';
import { mapGetters } from 'vuex';

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        selection: false,
        dialogClickModal: false,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        column: [
          {
            label: '单据类型',
            prop: 'sheetType',
            type: 'select',
            search: true,
            searchOrder: 1,
            dicUrl: '/api/blade-system/dict/dictionary?code=sheet_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '单据号',
            prop: 'sheetId',
            search: true,
            searchOrder: 2
          },
          {
            label: '操作人',
            prop: 'operatorname'
          },
          {
            label: '操作时间',
            prop: 'operatorDate'
          },
          {
            label: '单行号',
            prop: 'sheetDetailId'
          },
          {
            label: '物料编码',
            prop: 'partNo',
            search: true,
            searchOrder: 4
          },
          {
            label: '客户料号',
            prop: 'customerPartNo',
            search: true,
            searchOrder: 3
          },
          {
            label: '物料名称',
            prop: 'partNo',
            search: true,
            searchOrder: 5
          },
          {
            label: '物料规格',
            prop: 'partModel'
          },
          {
            label: '物料数量',
            prop: 'receiveQuantity'
          }
        ]
      },
      data: [],
      showDialog: false, //查看详情模态框是否显示
      detailLoading: false,
      detailData: [], //收料详情数据
      detailOption: {
        height: 'calc(100vh - 350px)',
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        selection: false,
        dialogClickModal: false,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        menu: false,
        refreshBtn: false,
        column: [
          {
            label: '包装条码',
            prop: 'packNo'
          },
          {
            label: '包装数量',
            prop: 'quantity'
          },
          {
            label: '是否尾箱',
            prop: 'isTailBox'
          },
          {
            label: '是否包装',
            prop: 'isPack'
          },
          {
            label: '是否报废',
            prop: 'scrap'
          },
          {
            label: '是否入库',
            prop: 'warehousing'
          }
        ]
      } //收料详情表格配置
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.material_add, false),
        viewBtn: this.vaildData(this.permission.material_view, false),
        delBtn: this.vaildData(this.permission.material_delete, false),
        editBtn: this.vaildData(this.permission.material_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getListTable(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    rowView(row) {
      this.showDialog = true;
    }
  }
};
</script>

<style></style>
